import jsonpath

from bazhuayun.cangkuguanli.base_api import BaseApi
from bazhuayun.utils.utils import Utils


class Product(BaseApi):
    def add_product(self, data):
        add_product_url = "http://api.wxorder.taover.com/v1/goodsbase/"
        req = {
            "method": "POST",
            "url": add_product_url,
            "headers": {"Authorization": self.get_token()},
            "json": data
        }
        r = self.send_api(req)
        return r

    def search_product(self, data):
        search_product_url = "http://api.wxorder.taover.com/v1/goodsbase"
        req ={
            "method": "GET",
            "url": search_product_url,
            "headers": {"Authorization": self.get_token()},
            "params": data
        }
        r = self.send_api(req)
        ids = jsonpath.jsonpath(r.json(), "$..goodsSkuList[*].id")[0]
        Utils.input_yaml_data(ids, "../config/product.yaml")
        return r

    def take_it_down(self):
        take_it_down_url = f"http://api.wxorder.taover.com/v1/wxordergoodsbasesku/updownstatus/0/sku/{Utils.get_yaml_data('../config/product.yaml')}"
        req = {
            "method": "PUT",
            "url": take_it_down_url,
            "headers": {"Authorization": self.get_token()}
        }
        r = self.send_api(req)
        return r

    def take_it_up(self):
        take_it_up_url = f"http://api.wxorder.taover.com/v1/wxordergoodsbasesku/updownstatus/1/sku/{Utils.get_yaml_data('../config/product.yaml')}"
        req = {
            "method": "PUT",
            "url": take_it_up_url,
            "headers": {"Authorization": self.get_token()}
        }
        r = self.send_api(req)
        return r














